<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>仓库管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../../../resources/lib/layui-v2.5.5/css/layui.css" media="all" />
    <link rel="stylesheet" href="../../../../resources/css/public.css" media="all" />
</head>
<body >
<div class="layuimini-container">
<!--搜索条件开始-->
	 <div class="layuimini-main">
		<fieldset class="layui-elem-field layuimini-search">
			<legend>搜索信息</legend>
			<div style="margin: 10px 10px 10px 10px">
				<form class="layui-form layui-form-pane" lay-filter="searchFrm" id="searchFrm" name="searchFrm" method="post">
					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">名称</label>
							<div class="layui-input-inline">
								<input type="text" name="name" id="name" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">备注</label>
							<div class="layui-input-inline">
								<input type="text" name="remark" id="remark" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<div class="layui-inline">
								<button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" lay-filter="doSearch" lay-submit="">查询</button>
								<button type="reset" class="layui-btn layui-icon layui-icon-refresh">重置</button>
							</div>
						</div>

					</div>
		</form>
			</div>
		</fieldset>
		<!--搜索条件结束-->
	
<!--搜索条件结束-->

<!--数据表格开始-->
	
		<table class="layui-hide" id="RoleTable" lay-filter="RoleTable"></table>
		<div id="RoleToolBar" style="display: none;">
			<button type="button" lay-event="addRole" class="layui-btn layui-btn-sm role_btn_add"><span class="layui-icon layui-icon-add-circle"></span>添加</button>
		</div>
		<div id="RoleRowBar" style="display: none">
			<button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
			<button type="button" lay-event="update" class="layui-btn layui-btn-sm "><span class="layui-icon layui-icon-set-sm"></span>更新</button>
			<button type="button" lay-event="selectMenu" class="layui-btn layui-btn-sm layui-btn-warm"><span class="layui-icon layui-icon-play"></span>分配权限</button>
		</div>
		<script type="text/html" id="availableTpl">
		  <input type="checkbox" name="available" value="{{d.id}}" lay-skin="switch" lay-text="可用|不可用" lay-filter="availableSwitch" {{ d.available == 1 ? 'checked' : '' }}>
		</script>
	</div>
	
</div>
<!--数据表格结束-->

<!--添加和修改的弹出层开始-->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv" >
    <form class="layui-form layui-form-pane"  lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-block">
				<!--隐藏一个ID-->
                <input type="hidden" name="id">
                <input type="text" name="name"  placeholder="请输入角色名称" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block"><!-- 这里新建一个编译器 -->
                <textarea class="layui-textarea" name="remark" lay-verify="content" id="content"></textarea>
            </div>
        </div>

        <div class="layui-form-item" style="text-align: center;">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release" lay-filter="doSubmit" lay-submit="">提交</button>
                <button type="reset" id="dataFrmResetBtn" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh" >重置</button>
            </div>
        </div>
    </form>
</div>
<!--添加和修改的弹出层结束-->

<script src="../../../../resources/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../../resources/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../../resources/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../../resources/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../../../resources/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../../resources/common/common.js" charset="utf-8"></script>
<script src="../../../../resources/common/selectLayer.js" charset="utf-8"></script>

<script>
    layui.use(['jquery','layer','form','table'],function () {
        var form=layui.form;
        var $=layui.jquery;
        var layer=layui.layer;
        var table=layui.table;


        var tableIndex = table.render({
            elem: '#RoleTable'
            ,url: api+'role/loadAllRole'
            ,toolbar: '#RoleToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '系统角色表'
            ,page: true
            ,cols: [ [
				{field:'id', title:'ID',align:'center'}
                ,{field:'name', title:'名称',align:'center'}
                ,{field:'createtime', title:'发布时间',align:'center'}
				,{field: 'available',  title: '是否可用', align: "center",width: 120,templet:'#availableTpl'}
				,{field:'remark', title:'备注',align:'center'}
                ,{fixed: 'right', title:'操作', toolbar: '#RoleRowBar',align:'center'}
            ] ]
            ,done:function(data,curr,count){
                //不是第一页如果返回的数据为0那么就返回上一页
                if(data.data.length==0&&curr!=1){
                    tableIndex.reload({
                        page:{
                            curr:curr-1
                        }
                    });
                }
            }
        });
		
		//监听可用操作
		 form.on('switch(availableSwitch)', function(obj){
		    $.post(api+"role/updateRole",{id:this.value,available:obj.elem.checked?1:0},function(res){
				  layer.msg(res.msg);
				  tableIndex.reload();
			 })
		   //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
		});

        //模糊查询
        form.on('submit(doSearch)',function(data){
            var params = $("#searchFrm").serialize();
            tableIndex.reload({
                url: api+"role/loadAllRole?" + params,
                page:{
                    curr:1
                }
            })
            return false;
        });

        //头工具栏事件
        table.on('toolbar(RoleTable)', function(obj){
            switch(obj.event){
                case 'addRole':
                    openAddRole();
                    break;
            };
        });


        //监听行工具条的事件
        table.on("tool(RoleTable)",function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'delete':
                    DeleteRole(data);
                    break;
                case 'update':
                    UpdateRole(data);
                    break;
                case 'selectMenu':
                    selectMenu(data);
                    break;
            };
        });

        var url;
        var mainIndex;

        //添加添加页面
        function openAddRole() {
            mainIndex = layer.open({
                type:1,
                title:'添加角色',
                content:$("#saveOrUpdateDiv"),
                area:['800px','550px'],
                success:function(index){
                    $("#dataFrm")[0].reset();
                    url= api+"role/addRole";
                }
            });
        }

        //更新页面
       function UpdateRole(data){
           mainIndex = layer.open({
               type:1,
               title:'修改角色',
               content:$("#saveOrUpdateDiv"),
               area:['800px','550px'],
               success:function(index){
                   form.val("dataFrm",data);
                   url= api+"role/updateRole";
               }
           });
        }

        //保存
        form.on("submit(doSubmit)",function(obj){
            //序列化表单数据
            var params=$("#dataFrm").serialize();
            $.post(url,params,function(obj){
                layer.msg(obj.msg);
                //关闭弹出层
                layer.close(mainIndex);
                //刷新数据 表格
                if (obj.code ==200){
                    tableIndex.reload();
                }
            })
        });

     

        //删除
        function  DeleteRole(data) {
            layer.confirm('你确定要删除这条数据吗?', {icon: 3, title:'提示'}, function(index){
                $.get( api+"role/deleteRole",{id:data.id},function(res){
                    if(res.code==200){
                        tableIndex.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });
        }

		//分配权限
		function selectMenu(data_table){
			//引入selectLayer.js
			openMenuAndPermissionSelecter(data_table);
		}
    })
</script>
</body>
</html>